#include "EconomyLand.h"
#include "EconomyAPI.h"
#include "EconomyPlugin.h"
#include "Managers/GameManager.h"
#include <fstream>
#include "spdlog/fmt/fmt.h"
#include "Expand/PluginsManager.h"
#include "Expand/Plugins/AuthPlugin.h"
#include "Network/Client.h"
#include "Player/PlayerData.h"
#include "Subsystems/SubsystemPlayers.h"

#include "Components/ComponentBody.h"
#include "Components/ComponentPlayer.h"
#include "Components/ComponentMiner.h"


namespace Economy
{
    EconomyLand::~EconomyLand() {}

    void EconomyLand::statisticsUserLandCount()
    {
        m_landCountMap.clear();
        for (auto it = m_landMap.begin(); it != m_landMap.end(); ++it)
        {
            it->second.landDataMutex.lock();

            std::vector<UserLand> &userVec = it->second.userLandVec;
            for (auto it2 = userVec.begin(); it2 != userVec.end(); ++it2)
            {
                auto landCountMapIt = m_landCountMap.find(it2->userGuid);
                if (landCountMapIt != m_landCountMap.end())
                {
                    landCountMapIt->second++;
                }
                else
                {
                    m_landCountMap.emplace(it2->userGuid ,1);
                }
            }

            it->second.landDataMutex.unlock();
        }
    }

    void EconomyLand::saveUserLandCount()
    {
        nlohmann::json jsonData = 
        { 
            { "landCountMap", m_landCountMap }
        };
        Utils::SaveTextFile(fmt::format("{}/EconomyLandCountMap.json", EconomyPlugin::PluginDataDirectory), jsonData.dump());
    }

    void EconomyLand::load()
    {
        m_economyAPI = GameSingleton::singletonPtr->registerSingleton<EconomyAPI>();

        m_subsystemPlayers = GameSingleton::singletonPtr->getSingleton<SubsystemPlayers>();
        m_authPlugin = GameSingleton::pluginsManager->findPlugin<AuthPlugin>();

        std::ifstream stream;
        stream.open(fmt::format("{}/EconomyLand.json", EconomyPlugin::PluginDataDirectory));

        if (stream.is_open())
        {
            nlohmann::json jsonData;
            stream >> jsonData;
            Utils::GetJsonValue<std::map<std::string, LandData>>(jsonData, m_landMap, "data", std::map<std::string, LandData>());
            Utils::GetJsonValue<uint32_t>(jsonData, m_landLimitCount, "landLimitCount", 1);

            statisticsUserLandCount();

            // Utils::GetJsonValue<std::map<GameGuid, uint32_t>>(jsonData, m_landCountMap, "landCountMap", std::map<GameGuid, uint32_t>());
        }
        stream.close();
    }

    void EconomyLand::save()
    {
        nlohmann::json jsonData = 
        { 
            { "data", m_landMap },
            { "landLimitCount", m_landLimitCount }
        };
        Utils::SaveTextFile(fmt::format("{}/EconomyLand.json", EconomyPlugin::PluginDataDirectory), jsonData.dump());
    }

    bool EconomyLand::getLandData(int32_t regionX, int32_t regionY, LandData *outLandData)
    {
        std::string key = fmt::format("{},{}", regionX, regionY);

        std::lock_guard<std::mutex> lk(m_landMapMutex);
        auto it = m_landMap.find(key);
        if(it != m_landMap.end())
        {
            outLandData = &(it->second);
            return true;
        }
        return false;
    }

    bool EconomyLand::getShellLandPrice(const Point3 &point, uint32_t &outMoney)
    {
        int32_t regionX = (point.posX >> 8);
        int32_t regionY = (point.posZ >> 8);
        Point3 targetPoint((point.posX & 0xFF), (point.posY & 0xFF), (point.posZ & 0xFF));

        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);

            // 优先判断正在售卖的领地
            const auto &shellLand = landDataPtr->shellLandVec;
            for (auto it = shellLand.begin(); it != shellLand.end(); ++it)
            {
                if( it->chunkPointStart.posX <= targetPoint.posX && 
                    it->chunkPointStart.posY <= targetPoint.posY && 
                    it->chunkPointStart.posZ <= targetPoint.posZ &&
                    it->chunkPointEnd.posX >= targetPoint.posX && 
                    it->chunkPointEnd.posY >= targetPoint.posY &&
                    it->chunkPointEnd.posZ >= targetPoint.posZ)
                {
                    outMoney = it->price;
                    return true;
                }
            }
        }
        return false;
    }

    bool EconomyLand::getLandUserGuid(const Point3 &point, GameGuid &outGameGuid)
    {
        int32_t regionX = (point.posX >> 8);
        int32_t regionY = (point.posZ >> 8);
        Point3 targetPoint((point.posX & 0xFF), (point.posY & 0xFF), (point.posZ & 0xFF));

        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);

            // 优先判断正在售卖的领地
            const auto &userLand = landDataPtr->userLandVec;
            for(auto it = userLand.begin(); it != userLand.end(); ++it)
            {
                if( it->chunkPointStart.posX <= targetPoint.posX && 
                    it->chunkPointStart.posY <= targetPoint.posY && 
                    it->chunkPointStart.posZ <= targetPoint.posZ &&
                    it->chunkPointEnd.posX >= targetPoint.posX && 
                    it->chunkPointEnd.posY >= targetPoint.posY &&
                    it->chunkPointEnd.posZ >= targetPoint.posZ)
                {
                    outGameGuid = it->userGuid;
                    return true;
                }
            }
        }
        return false;
    }

    bool EconomyLand::haveAuth(int32_t regionX, int32_t regionY, const GameGuid &userGuid)
    {
        if(m_authPlugin->getLevelByGuid(userGuid) >= 100)
        {
            return true;
        }
        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);
            for(auto it = landDataPtr->authUserSet.begin(); it != landDataPtr->authUserSet.end(); ++it)
            {
                if(*it == userGuid)
                {
                    return true;
                }
            }
            return false;
        }
        return true;
    }

    bool EconomyLand::addAuth(int32_t regionX, int32_t regionY, const GameGuid &userGuid)
    {
        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);
            landDataPtr->authUserSet.insert(userGuid);
            return true;
        }
        return false;
    }

    bool EconomyLand::removeAuth(int32_t regionX, int32_t regionY, const GameGuid &userGuid)
    {
        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);
            auto it = landDataPtr->authUserSet.find(userGuid);
            if (it != landDataPtr->authUserSet.end())
            {
                landDataPtr->authUserSet.erase(it);
                return true;
            }
        }
        return false;
    }

    bool EconomyLand::getLandDataMode(int32_t regionX, int32_t regionY, LandType &outType)
    {
        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            outType = landDataPtr->mode;
            return true;
        }
        return false;
    }

    void EconomyLand::setLandDataMode(int32_t regionX, int32_t regionY, LandType type)
    {
        std::string key = fmt::format("{},{}", regionX, regionY);
        m_landMapMutex.lock();

        auto it = m_landMap.find(key);
        if(it != m_landMap.end())
        {
            it->second.mode = type;
        }
        else
        {
            m_landMap.emplace(key, type);
        }

        m_landMapMutex.unlock();
    }

    bool EconomyLand::removeLandData(int32_t regionX, int32_t regionY)
    {
        std::string key = fmt::format("{},{}", regionX, regionY);

        std::lock_guard<std::mutex> lk(m_landMapMutex);
        auto it = m_landMap.find(key);
        if(it != m_landMap.end())
        {
            m_landMap.erase(it);
            return true;
        }
        return false;
    }

    bool EconomyLand::shellLandToUserLand(const Point3& point, const GameGuid &userGuid)
    {
        int32_t regionX = (point.posX >> 8);
        int32_t regionY = (point.posZ >> 8);
        Point3 targetPoint((point.posX & 0xFF), (point.posY & 0xFF), (point.posZ & 0xFF));

        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);

            auto &shellLand = landDataPtr->shellLandVec;
            for (auto it = shellLand.begin(); it != shellLand.end(); ++it)
            {
                if( it->chunkPointStart.posX <= targetPoint.posX && 
                    it->chunkPointStart.posY <= targetPoint.posY && 
                    it->chunkPointStart.posZ <= targetPoint.posZ &&
                    it->chunkPointEnd.posX >= targetPoint.posX && 
                    it->chunkPointEnd.posY >= targetPoint.posY &&
                    it->chunkPointEnd.posZ >= targetPoint.posZ)
                {
                    landDataPtr->userLandVec.emplace_back(userGuid, it->chunkPointStart, it->chunkPointEnd);
                    shellLand.erase(it);
                    
                    // 添加计数
                    auto it2 = m_landCountMap.find(userGuid);
                    if (it2 == m_landCountMap.end())
                    {
                        m_landCountMap.emplace(userGuid, 1);
                    }
                    else
                    {
                        it2->second++;
                    }
                    return true;
                }
            }
        }
        return false;
    }

    bool EconomyLand::userLandSetNewUser(const Point3& point, const GameGuid &userGuid)
    {
        int32_t regionX = (point.posX >> 8);
        int32_t regionY = (point.posZ >> 8);
        Point3 targetPoint((point.posX & 0xFF), (point.posY & 0xFF), (point.posZ & 0xFF));

        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);

            auto &userLand = landDataPtr->userLandVec;
            for(auto it = userLand.begin(); it != userLand.end(); ++it)
            {
                if( it->chunkPointStart.posX <= targetPoint.posX && 
                    it->chunkPointStart.posY <= targetPoint.posY && 
                    it->chunkPointStart.posZ <= targetPoint.posZ &&
                    it->chunkPointEnd.posX >= targetPoint.posX && 
                    it->chunkPointEnd.posY >= targetPoint.posY &&
                    it->chunkPointEnd.posZ >= targetPoint.posZ)
                {
                    // 减少计数
                    auto it2 = m_landCountMap.find(it->userGuid);
                    if (it2 == m_landCountMap.end())
                    {
                        m_landCountMap.emplace(it->userGuid, 0);
                    }
                    else
                    {
                        it2->second--;
                    }

                    // 替换领地所有者
                    it->userGuid = userGuid;

                    // 增加计数
                    auto it3 = m_landCountMap.find(it->userGuid);
                    if (it3 == m_landCountMap.end())
                    {
                        m_landCountMap.emplace(it->userGuid, 0);
                    }
                    else
                    {
                        it3->second--;
                    }
                    return true;
                }
            }
        }
        return false;
    }

    UserLand *EconomyLand::getUserLand(const Point3 &point)
    {
        int32_t regionX = (point.posX >> 8);
        int32_t regionY = (point.posZ >> 8);
        Point3 targetPoint((point.posX & 0xFF), (point.posY & 0xFF), (point.posZ & 0xFF));

        LandData *landDataPtr;
        if(getLandData(regionX, regionY, landDataPtr) == true)
        {
            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);

            auto &userLand = landDataPtr->userLandVec;
            for(auto it = userLand.begin(); it != userLand.end(); ++it)
            {
                if( it->chunkPointStart.posX <= targetPoint.posX && 
                    it->chunkPointStart.posY <= targetPoint.posY && 
                    it->chunkPointStart.posZ <= targetPoint.posZ &&
                    it->chunkPointEnd.posX >= targetPoint.posX && 
                    it->chunkPointEnd.posY >= targetPoint.posY &&
                    it->chunkPointEnd.posZ >= targetPoint.posZ)
                {
                    UserLand *userLand = &(*it);
                    return userLand;
                }
            }
        }
        return nullptr;
    }



    static const std::string titleText = "领地";

    constexpr const char *help1 = "\n"
                                  "/land    -查看自己脚下领地是谁的或价格及租金\n"
                                  "/land buy    -租用脚下的领地\n"
                                  "/land help    -查看领地有关的指令\n"
                                  "/land invite add (玩家)    -站在自己领地上，然后给对应玩家权限\n"
                                  "/land invite remove (玩家)    -站在自己领地上，然后移除对应玩家权限\n"
                                  "/land invite clear    -站在自己领地上，然后清空其他玩家权限\n"
                                  "/land invite list    -查看自己领地上，哪些玩家有权限";

    constexpr const char *help2 = "\n"
                                  "/land    -查看自己脚下领地是谁的或价格及租金\n"
                                  "/land buy    -租用脚下的领地\n"
                                  "/land help    -查看领地有关的指令\n"
                                  "/land invite add (玩家)    -站在自己领地上，然后给对应玩家权限\n"
                                  "/land invite remove (玩家)    -站在自己领地上，然后移除对应玩家权限\n"
                                  "/land invite clear    -站在自己领地上，然后清空其他玩家权限\n"
                                  "/land invite list    -查看自己领地上，哪些玩家有权限\n"

                                  "/land give (玩家)    -将自己脚下的强制租给对应的玩家，不需要首付\n"
                                  "/land set (玩家)    -修改自己脚下玩家领地的所有者\n"
                                  "/land region set (模式)    -修改自己脚下区域领地的模式\n"
                                  "/land region remove    -移除脚下的区域领地数据，玩家租用数据一并移除\n"
                                  "/land auth add (玩家)    -给对应玩家现自己脚下区域领地的权限\n"
                                  "/land auth remove (玩家)    -移除脚下区域领地对应玩家的权限\n"
                                  "/land auth clear    -清空脚下区域领地的权限表\n"
                                  "/land auth list    -查看脚下区域领地上，哪些玩家有权限\n"
                                  "/land count    -重新计数玩家领地数\n"
                                  "/land countsave    -保存玩家领地数的数据";

    static const std::string normalHelp = help1;
    static const std::string proHelp = help2;

    void EconomyLand::sendHelpMessage(IPlugin::ChatObj &chatObj)
    {
        if (chatObj.m_isTerminal == true || m_authPlugin->getLevelByGuid(chatObj.m_client->guid) >= 100)
        {
            chatObj.sendMessage(titleText, proHelp);
        }
        else
        {
            chatObj.sendMessage(titleText, normalHelp);
        }
    }

    void EconomyLand::sendGuidMessage(const GameGuid& gameGuid, const std::string &message)
    {
        PlayerDataPtr playerData = m_subsystemPlayers->getPlayerDataByGuid(gameGuid);
        if(playerData != nullptr)
        {
            IPlugin::ChatObj chatObj(playerData->client, false);
            chatObj.sendMessage(titleText, message);
        }
    }

    void EconomyLand::sendPlayerMessage(PlayerDataPtr &playerDataPtr, const std::string &message)
    {
        // if (playerDataPtr != nullptr)
        {
            IPlugin::ChatObj chatObj(playerDataPtr->client, false);
            chatObj.sendMessage(titleText, message);
        }
    }

    void EconomyLand::processCmd(IPlugin::ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if (chatObj.m_isTerminal == true)
        {
            chatObj.sendMessage(titleText, "控制台不能使用该指令");
            return;
        }

        Vector3 position;
        chatObj.m_client->playerData->componentPlayer->componentBody->getPosition(position);
        Point3 point = position.toPoint3();

        if(cmdDataVec.size() == 1)
        {
            uint32_t money;
            GameGuid gameGuid;
            if (getShellLandPrice(point, money))
            {
                chatObj.sendMessage(titleText, fmt::format("你脚下的领出售价格为 {} $", money));
                return;
            }
            else if (getLandUserGuid(point, gameGuid))
            {
                std::string username;
                if(m_subsystemPlayers->playerGuidGetName(gameGuid, username))
                {
                    chatObj.sendMessage(titleText, fmt::format("你脚下的领地是 {} 的", username));
                }
                else
                {
                    int32_t regionX = point.posX >> 8;
                    int32_t regionY = point.posZ >> 8;
                    Point3 targetPoint((point.posX & 0xFF), (point.posY & 0xFF), (point.posZ & 0xFF));
                    Log::Error(fmt::format("玩家领地 guid : {} 查找不到玩家[{},{}]({})", gameGuid.toString(), regionX, regionY, targetPoint.toString()));
                }
                return;
            }
            chatObj.sendMessage(titleText, "没有领地数据");
            return;
        }
        else if(cmdDataVec.size() == 2)
        {
            if (cmdDataVec[1] == "buy")
            {
                auto it = m_landCountMap.find(chatObj.m_client->playerData->playerGuid);
                if (it != m_landCountMap.end())
                {
                    if (it->second >= m_landLimitCount)
                    {
                        chatObj.sendMessage(titleText, "你的领地个数已经达到限制");
                        return;
                    }
                }

                uint32_t price;
                if (getShellLandPrice(point, price))
                {
                    uint32_t money = m_economyAPI->getGuidMoney(chatObj.m_client->guid);
                    if (money >= price)
                    {
                        m_economyAPI->decGuidMoney(chatObj.m_client->guid, price);
                        if (shellLandToUserLand(point, chatObj.m_client->guid))
                        {
                            chatObj.sendMessage(titleText, fmt::format("成功购买到领地，花费 {} $", price));
                        }
                        else
                        {
                            chatObj.sendMessage(titleText, fmt::format("花费 {} $，但是购买领地失败了，请截图找管理员", price));
                        }
                    }
                    else
                    {
                        chatObj.sendMessage(titleText, fmt::format("你的金钱不足以购买领地，购买该领地需要 {} $， 而你则有 {} $", price, money));
                    }
                    return;
                }
                chatObj.sendMessage(titleText, "这里没有领地出售");
                return;
            }
            else if (cmdDataVec[1] == "region")
            {
                int32_t regionX = point.posX >> 8;
                int32_t regionY = point.posZ >> 8;
                
                LandType type;
                if (getLandDataMode(regionX, regionY, type))
                {
                    chatObj.sendMessage(titleText, fmt::format("区域领地模式为：{}", (uint8_t)type));
                }
                else
                {
                    chatObj.sendMessage(titleText, "这里没有领地模式");
                }
            }
            else if (cmdDataVec[1] == "count")
            {
                uint32_t authLevel = authLevel = m_authPlugin->getLevelByGuid(chatObj.m_client->guid);
                if (authLevel >= 100)
                {
                    statisticsUserLandCount();
                    chatObj.sendMessage(titleText, "重新计数完成");
                }
            }
            else if (cmdDataVec[1] == "countsave")
            {
                uint32_t authLevel = authLevel = m_authPlugin->getLevelByGuid(chatObj.m_client->guid);
                if (authLevel >= 100)
                {
                    saveUserLandCount();
                    chatObj.sendMessage(titleText, "保存玩家领地数据完成");
                }
            }
        }
        else if(cmdDataVec.size() == 3)
        {
            uint32_t authLevel = authLevel = m_authPlugin->getLevelByGuid(chatObj.m_client->guid);
            if (authLevel >= 100)
            {
                if (cmdDataVec[1] == "set")
                {
                    std::vector<std::shared_ptr<PlayerData>> playerVec;
                    m_subsystemPlayers->getPlayerDataByName(cmdDataVec[2], playerVec);
                    if(playerVec.size() == 0)
                    {
                        chatObj.sendMessage(titleText, fmt::format("玩家 {} 貌似不在线", cmdDataVec[2]));
                    }
                    else if (playerVec.size() == 1)
                    {
                        if (shellLandToUserLand(point, playerVec[0]->playerGuid))
                        {
                            chatObj.sendMessage(titleText, fmt::format("成功将售卖的领地给了 {}", playerVec[0]->username));
                        }
                        else if (userLandSetNewUser(point, playerVec[0]->playerGuid))
                        {
                            chatObj.sendMessage(titleText, fmt::format("成功将领地给了 {}", playerVec[0]->username));
                        }
                        else
                        {
                            chatObj.sendMessage(titleText, "没有领地数据");
                        }
                    }
                    else if (playerVec.size() > 1)
                    {
                        chatObj.sendMessage(titleText, fmt::format("有多个名字为 {} 的玩家，无法设置领地"));
                    }
                    return;
                }
                else if (cmdDataVec[1] == "auth")
                {
                    if (cmdDataVec[2] == "clear")
                    {
                        int32_t regionX = point.posX >> 8;
                        int32_t regionY = point.posZ >> 8;

                        LandData *landDataPtr;
                        if(getLandData(regionX, regionY, landDataPtr) == true)
                        {
                            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);

                            landDataPtr->authUserSet.clear();
                            chatObj.sendMessage(titleText, fmt::format("成功清理 [{},{}] 区域领地的权限", regionX, regionY));
                        }
                        else
                        {
                            chatObj.sendMessage(titleText, "查找不到区域领地数据");
                        }
                        return;
                    }
                    else if (cmdDataVec[2] == "list")
                    {
                        int32_t regionX = point.posX >> 8;
                        int32_t regionY = point.posZ >> 8;

                        LandData *landDataPtr;
                        if(getLandData(regionX, regionY, landDataPtr) == true)
                        {
                            std::lock_guard<std::mutex> lk(landDataPtr->landDataMutex);

                            std::string output = "区域领地的权限玩家为：";
                            for (GameGuid gameGuid : landDataPtr->authUserSet)
                            {
                                std::string name;
                                if (m_subsystemPlayers->playerGuidGetName(gameGuid, name))
                                {
                                    output += fmt::format("\n[{}]{}", gameGuid.toString(), name);
                                }
                                else
                                {
                                    output += fmt::format("\n{}", gameGuid.toString());
                                }
                            }
                        }
                        else
                        {
                            chatObj.sendMessage(titleText, "查找不到区域领地数据");
                        }
                        return;
                    }
                }
            }
            else
            {
                if (cmdDataVec[1] == "invite")
                {
                    if (cmdDataVec[2] == "clear")
                    {
                        UserLand* userLand = getUserLand(point);
                        if (userLand != nullptr)
                        {
                            if (userLand->userGuid == chatObj.m_client->guid)
                            {
                                userLand->inviteUserSet.clear();
                                chatObj.sendMessage(titleText, "成功清理掉有权限的玩家");
                            }
                            else
                            {
                                chatObj.sendMessage(titleText, "该领地不是你的");
                            }
                        }
                        else
                        {
                            chatObj.sendMessage(titleText, "没有领地数据");
                        }
                        return;
                    }
                    else if (cmdDataVec[2] == "list")
                    {
                        UserLand* userLand = getUserLand(point);
                        if (userLand != nullptr)
                        {
                            if (userLand->userGuid == chatObj.m_client->guid)
                            {
                                std::string output = "区域领地的权限玩家为：";
                                for (GameGuid gameGuid : userLand->inviteUserSet)
                                {
                                    std::string name;
                                    if (m_subsystemPlayers->playerGuidGetName(gameGuid, name))
                                    {
                                        output += fmt::format("\n[{}]{}", gameGuid.toString(), name);
                                    }
                                    else
                                    {
                                        output += fmt::format("\n{}", gameGuid.toString());
                                    }
                                }
                            }
                            else
                            {
                                chatObj.sendMessage(titleText, "该领地不是你的");
                            }
                        }
                        else
                        {
                            chatObj.sendMessage(titleText, "没有领地数据");
                        }
                        return;
                    }
                }
            }
        }
        else if(cmdDataVec.size() >= 4)
        {
            uint32_t authLevel = authLevel = m_authPlugin->getLevelByGuid(chatObj.m_client->guid);
            if (authLevel >= 100)
            {
                if (cmdDataVec[1] == "region")
                {
                    if (cmdDataVec[2] == "set")
                    {
                        int32_t regionX = point.posX >> 8;
                        int32_t regionY = point.posZ >> 8;

                        uint8_t mode = atoi(cmdDataVec[3].c_str());
                        switch (mode)
                        {
                        case 0:
                            setLandDataMode(regionX, regionY, LandType::None);
                            chatObj.sendMessage(titleText, "成功将区域的模式改为 无限制");
                            break;
                        case 1:
                            setLandDataMode(regionX, regionY, LandType::NeedBuyLand);
                            chatObj.sendMessage(titleText, "成功将区域的模式改为 需要购买领地才能破坏");
                            break;
                        case 2:
                            setLandDataMode(regionX, regionY, LandType::LimitLand);
                            chatObj.sendMessage(titleText, "成功将区域的模式改为 非权限玩家或管理员不可破坏");
                            break;
                        default:
                            chatObj.sendMessage(titleText, "成功将区域的模式改为 没有该模式");
                            break;
                        }
                        return;
                    }
                }
                else if (cmdDataVec[1] == "auth")
                {
                    if (cmdDataVec[2] == "add" || cmdDataVec[2] == "remove")
                    {
                        std::vector<std::shared_ptr<PlayerData>> playerVec;
                        m_subsystemPlayers->getPlayerDataByName(cmdDataVec[3], playerVec);
                        if(playerVec.size() == 0)
                        {
                            chatObj.sendMessage(titleText, fmt::format("玩家 {} 貌似不在线", cmdDataVec[3]));
                        }
                        else if (playerVec.size() == 1)
                        {
                            int32_t regionX = point.posX >> 8;
                            int32_t regionY = point.posZ >> 8;

                            if (cmdDataVec[2] == "add")
                            {
                                if (addAuth(regionX, regionY, playerVec[0]->playerGuid))
                                {
                                    chatObj.sendMessage(titleText, fmt::format("成功给予玩家 {}，领地区域的权限", cmdDataVec[3]));
                                }
                                else
                                {
                                    chatObj.sendMessage(titleText, fmt::format("给予玩家 {}，领地区域的权限失败", cmdDataVec[3]));
                                }
                            }
                            else
                            {
                                if (removeAuth(regionX, regionY, playerVec[0]->playerGuid))
                                {
                                    chatObj.sendMessage(titleText, fmt::format("成功移除玩家 {}，领地区域的权限", cmdDataVec[3]));
                                }
                                else
                                {
                                    chatObj.sendMessage(titleText, fmt::format("移除玩家 {}，领地区域的权限失败", cmdDataVec[3]));
                                }
                            }
                        }
                        else if (playerVec.size() > 1)
                        {
                            chatObj.sendMessage(titleText, fmt::format("有多个名字为 {} 的玩家，无法设置领地"));
                        }
                        return;
                    }
                }
                else if (cmdDataVec[1] == "invite")
                {
                    if (cmdDataVec[2] == "add" || cmdDataVec[2] == "remove")
                    {
                        std::vector<std::shared_ptr<PlayerData>> playerVec;
                        m_subsystemPlayers->getPlayerDataByName(cmdDataVec[3], playerVec);
                        if(playerVec.size() == 0)
                        {
                            chatObj.sendMessage(titleText, fmt::format("玩家 {} 貌似不在线", cmdDataVec[3]));
                        }
                        else if (playerVec.size() == 1)
                        {
                            UserLand* userLand = getUserLand(point);
                            if (userLand != nullptr)
                            {
                                if (userLand->userGuid == chatObj.m_client->guid)
                                {
                                    if (cmdDataVec[2] == "add")
                                    {
                                        userLand->inviteUserSet.insert(playerVec[0]->playerGuid);
                                        chatObj.sendMessage(titleText, fmt::format("成功给予玩家 {}，领地的权限", cmdDataVec[3]));
                                    }
                                    else
                                    {
                                        auto it = userLand->inviteUserSet.find(playerVec[0]->playerGuid);
                                        if (it != userLand->inviteUserSet.end())
                                        {
                                            userLand->inviteUserSet.erase(it);
                                            chatObj.sendMessage(titleText, fmt::format("成功移除玩家 {}，领地的权限", cmdDataVec[3]));
                                        }
                                        else
                                        {
                                            chatObj.sendMessage(titleText, fmt::format("移除玩家 {}，领地的权限失败", cmdDataVec[3]));
                                        }
                                    }
                                }
                                else
                                {
                                    chatObj.sendMessage(titleText, "该领地不是你的");
                                }
                            }
                            else
                            {
                                chatObj.sendMessage(titleText, "没有领地数据");
                            }
                        }
                        else if (playerVec.size() > 1)
                        {
                            chatObj.sendMessage(titleText, fmt::format("有多个名字为 {} 的玩家，无法设置领地权限"));
                        }
                        return;
                    }
                }
            }
        }
        sendHelpMessage(chatObj);
    }
}




                                //   "/land invite add (玩家)    -站在自己领地上，然后给对应玩家权限\n"
                                //   "/land invite remove (玩家)    -站在自己领地上，然后移除对应玩家权限\n"
                                //   "/land invite clear    -站在自己领地上，然后清空其他玩家权限\n"
                                //   "/land invite list    -查看自己领地上，哪些玩家有权限";


                                //   "/land set (玩家)    -修改自己脚下玩家领地的者\n"
                                //   "/land region set (模式)    -修改自己脚下区域领地的模式\n"
                                //   "/land region remove    -移除脚下的区域领地数据，玩家租用数据一并移除\n"
                                //   "/land auth add (玩家)    -给对应玩家现自己脚下区域领地的权限\n"
                                //   "/land auth remove (玩家)    -移除脚下区域领地对应玩家的权限\n"
                                //   "/land auth clear    -清空脚下区域领地的权限表\n"
                                //   "/land auth list    -查看脚下区域领地上，哪些玩家有权限";

                                
